package com.hl.yyx.modules.ums.controller;

import com.hl.yyx.common.log.LogAnnotation;
import com.hl.yyx.common.response.ListResponse;
import com.hl.yyx.common.response.ObjectResponse;
import com.hl.yyx.common.response.Response;
import com.hl.yyx.dto.IdDTO;
import com.hl.yyx.dto.SearchFilters;
import com.hl.yyx.modules.ums.dto.AssignRoleDTO;
import com.hl.yyx.modules.ums.dto.InitMenuDTO;
import com.hl.yyx.modules.ums.dto.UmsAdminLoginParam;
import com.hl.yyx.modules.ums.dto.UpdatePassDTO;
import com.hl.yyx.modules.ums.model.UmsAdmin;
import com.hl.yyx.modules.ums.service.UmsAdminService;
import com.hl.yyx.modules.ums.service.UmsMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author hl243695czyn
 * @since 2022-06-20
 */
@RestController
@RequestMapping("/admin/admin")
@Api(tags = "管理员管理", description = "管理员管理")
public class UmsAdminController {

    @Autowired
    private UmsAdminService umsAdminService;

    @Autowired
    private UmsMenuService menuService;

    @Value("${jwt.tokenHead}")
    private String tokenHead;

    // 登录
    @LogAnnotation()
    @ApiOperation("登录")
    @PostMapping(value = "/login")
    public Response login(@RequestBody UmsAdminLoginParam loginParam, HttpServletRequest request) {
        String token = umsAdminService.login(loginParam.getUsername(), loginParam.getPassword(), request);
        // 根据当前用户获取对应的菜单
        String userId = umsAdminService.getCurrentAdmin().getId();
        UmsAdmin userInfo = umsAdminService.getCurrentAdmin();
        List<InitMenuDTO> menuList = menuService.getMenuListByUserId(userId);
        HashMap<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("userInfo", userInfo);
        tokenMap.put("menuList", menuList);
        tokenMap.put("tokenHead", tokenHead);
        return new ObjectResponse<>(tokenMap);
    }

    // 分页
    @LogAnnotation()
    @ApiOperation("分页查询")
    @PostMapping(value = "/page")
    public Response page(@RequestBody SearchFilters<UmsAdmin> searchFilters) {
        return umsAdminService.pageList(searchFilters);
    }

    // 新增
    @LogAnnotation()
    @ApiOperation("新增管理员")
    @PostMapping(value = "/create")
    public Response create(@Valid @RequestBody UmsAdmin umsAdmin) {
        return umsAdminService.create(umsAdmin);
    }

    // 更新
    @LogAnnotation()
    @ApiOperation("更新管理员")
    @PostMapping(value = "/update")
    public Response update(@Valid @RequestBody UmsAdmin umsAdmin) {
        return umsAdminService.updateAdmin(umsAdmin);
    }

    // 删除
    @LogAnnotation()
    @ApiOperation("删除管理员")
    @PostMapping(value = "/delete")
    public Response delete(@Valid @RequestBody IdDTO idDTO) {
        return umsAdminService.delete(idDTO);
    }

    // 获取全部
    @LogAnnotation()
    @ApiOperation("获取全部管理员")
    @PostMapping(value = "/list")
    public Response list() {
        return new ListResponse<>(umsAdminService.list());
    }

    // 查看
    @LogAnnotation()
    @ApiOperation("查看管理员")
    @PostMapping(value = "/view")
    public Response view(@Valid @RequestBody IdDTO idDTO) {
        return umsAdminService.view(idDTO);
    }

    // 修改密码
    @LogAnnotation()
    @ApiOperation("修改密码")
    @PostMapping(value = "/updatePass")
    public Response updatePass(@RequestBody UpdatePassDTO updatePassDTO) {
        return new ObjectResponse<>(umsAdminService.updatePass(updatePassDTO));
    }

    // 获取用户已分配的角色
    @LogAnnotation()
    @ApiOperation("获取用户已分配的角色")
    @PostMapping(value = "/view-assignRole")
    public Response viewAssignRole(@Valid @RequestBody IdDTO idDTO) {
        return new ObjectResponse<>(umsAdminService.viewAssignRole(idDTO));
    }

    // 分配角色
    @LogAnnotation()
    @ApiOperation("分配角色")
    @PostMapping(value = "/set-assignRole")
    public Response assignRole(@Valid @RequestBody AssignRoleDTO assignRoleDTO) {
        return new ObjectResponse<>(umsAdminService.setAssignRole(assignRoleDTO));
    }
}

